<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Withdraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WithdrawController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Withdraw(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid')->modal(function (Grid\Displayers\Modal $modal) {
                $modal->title('用户信息');
                return User::make()->payload(['id' => $this->uid]);
            });
            $grid->column('money');
            $grid->column('card_id')->modal(function (Grid\Displayers\Modal $modal) {
                $modal->title('银行卡信息');
                    return Card::make()->payload(['id' => $this->card_id]);
            });
            $grid->column('state')->display(function ($status) {
                if ($status == 0) {
                    return "<span style='color:red'>待审核</span>";
                }
                if ($status == 1) {
                    return "<span style='color:green'>通过</span>";
                }
                if ($status == 2) {
                    return "<span style='color:red'>拒绝</span>";
                }
            });
            $grid->column('msg');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('uid');
                $filter->equal('state')->select(['0'=>'待审核', '1'=>'通过', '2'=>'拒绝']);
            });
            $grid->export();

            $grid->disableCreateButton();
//            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableBatchActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Withdraw(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('money');
            $show->field('card_id');
            $show->field('state');
            $show->field('msg');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Withdraw(), function (Form $form) {
            $form->display('id');
            $form->display('uid');
            $form->display('money');
            $form->display('card_id');
            $checks = [
                '0' => '待审核',
                '1' => '通过',
                '2' => '拒绝',
            ];
            $form->select('state')->options($checks);
            $form->text('msg');

            $form->display('created_at');
            $form->display('updated_at');

            $form->disableDeleteButton();
            $form->disableViewButton();

            $form->saving(function (Form $form) {
                $status = $form->model()->state;
                $money = $form->model()->money;
                $uid = $form->model()->uid;
                $status1 = $form->state;
                $reason = $form->msg;
                if (($status == 1 || $status == 2) && $status1 != $status) {
                    return $form->response()->error('该申请已经审核，不可重复操作');
                } elseif($status1 == 2 && $status==0){
                    if(!$reason){
                        return $form->response()->error('请填写拒绝原因');
                    }
                    try {
                        $user = \App\Models\User::where('id', $uid)->firstOrFail();
                        $user->balance += $money;
                        $user->save();
                    } catch (\Exception $e){
                        Log::error('审核失败');
                        Log::error($e->getMessage(), $e->getTrace());
                        return $form->response()->error('审核失败');
                    }
                }
            });
        });
    }
}
